﻿'Copyright (c) 2011 Kraigose Studios (Kraig C)

'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.

Imports System.IO

Module modMain

    Dim currDir As New DirectoryInfo(Directory.GetCurrentDirectory())
    Dim totalArtFiles As Integer
    Dim artDir As DirectoryInfo
    Dim pngDir As DirectoryInfo
    Dim artFiles As FileInfo()
    Dim pngFiles As FileInfo()
    Dim iniFiles As FileInfo()
    Dim palDir As DirectoryInfo
    Dim palFile As FileInfo()

    Public validMode As Boolean = False
    Public modeOp As Integer = 0           ' 0 = invalid, 1 = export, 2 = import
    Public helpMode As Boolean = False
    Public errors As Boolean = False
    Public verbose As Boolean = False

    Sub Main()
        Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs

        If CommandLineArgs.Count >= 4 Then
            Dim firstOption As String = CommandLineArgs(0)  ' Mode (-export, -import or -help)
            Dim secondOption As String = CommandLineArgs(1) ' Directory for ART files (relative to current working directory)
            Dim thirdOption As String = CommandLineArgs(2)  ' Directory for PNG tiles and INI files (relative to current working directory)
            Dim fourthOption As String = CommandLineArgs(3) ' File for PALETTE.DAT (relative to current working directory)
            Dim finalOption As String = CommandLineArgs(4)  ' Verbose Flag (-v or -verbose

            Dim palstream As FileStream     ' FileStream for Palette

            firstOption = firstOption.ToLower()
            finalOption = finalOption.ToLower()

            Console.WriteLine("Art2Png Tool By Kraigose" + vbNewLine)

            If firstOption = "-export" Then
                modeOp = 1
                Console.WriteLine("Export Mode" + vbNewLine)
                validMode = True
            ElseIf firstOption = "-import" Then
                modeOp = 2
                Console.WriteLine("Import Mode" + vbNewLine)
                validMode = True
            ElseIf firstOption = "-help" Or firstOption = "-?" Then
                DisplayHelp()
                helpMode = True
                validMode = True
            Else
                DisplayHelp()
                helpMode = True
                validMode = False
            End If

            ' check if we want verbose diagnostics
            If finalOption = "-v" Or finalOption = "-verbose" Then
                verbose = True
            End If

            ' If the mode is a valid mode we will check which mode it is in.
            If validMode = True Then
                If helpMode = False Then

                    'get the proper folders
                    artDir = New DirectoryInfo(currDir.FullName + "\" + secondOption)   ' For ART files
                    pngDir = New DirectoryInfo(currDir.FullName + "\" + thirdOption)    ' For PNG files
                    palDir = New DirectoryInfo(currDir.FullName + "\" + fourthOption)   ' For the PALETTE.DAT file

                    If verbose Then
                        Console.WriteLine("ART Directory is: " + vbTab + vbTab + artDir.FullName)
                        Console.WriteLine("PNG-INI Directory is: " + vbTab + vbTab + pngDir.FullName)
                        Console.WriteLine("Palette Directory is: " + vbTab + vbTab + palDir.FullName)
                    End If

                    ' fetch the files
                    artFiles = artDir.GetFiles("*.art")         ' Fetch all ART files
                    pngFiles = pngDir.GetFiles("tile*.png")     ' Fetch all tile*.png files
                    iniFiles = pngDir.GetFiles("*.ini")         ' Fetch all *ini files
                    palFile = palDir.GetFiles("PALETTE.DAT")    ' Get the lone PALETTE.DAT stored

                    If verbose Then
                        Console.WriteLine("ART Files: {0}", artFiles.Length.ToString())
                        Console.WriteLine("PNG Files: {0}", pngFiles.Length.ToString())
                    End If

                    If modeOp = 1 Then
                        If artFiles.Length < 1 Then
                            Console.WriteLine("ERRORS: No ART files to be found. Press any key to exit")
                            Console.ReadKey()
                            errors = True
                        End If
                        If errors = False Then
                            Dim Filename As FileInfo
                            palstream = New FileStream(palFile(0).FullName, FileMode.Open, FileAccess.Read)
                            OpenPal(palstream)
                            For Each Filename In artFiles
                                Dim tmpf As String = Filename.Name.ToUpper()
                                tmpf = tmpf.Remove(0, 5)
                                tmpf = tmpf.Remove(3, 4)
                                currFileNum = Integer.Parse(tmpf)
                                Dim files As New FileStream(Filename.FullName, FileMode.Open)
                                Dim cArtF As ArtFile
                                OpenArt(files, cArtF)
                                DumpINI(cArtF, pngDir.FullName)
                                DumpPNGs(cArtF, pngDir.FullName)
                            Next
                            Console.WriteLine()
                            Console.WriteLine("Done Exporting. Press Any Key To Exit")
                        End If
                    ElseIf modeOp = 2 Then
                        If pngFiles.Length < 1 Then
                            Console.WriteLine("ERRORS: No PNG files to be found. Press any key to exit")
                            Console.ReadKey()
                            errors = True
                        End If
                    End If
                Else
                    AnyKeyToExit()
                End If
            Else
                Console.WriteLine("Invalid Mode. Press any Key to Continue")
                Console.ReadKey()
            End If
        Else
            DisplayHelp()
            AnyKeyToExit()
        End If
    End Sub

    ' This was being used so much, I figured refactoring it would make it easier.
    ' This displays the help text.
    Private Sub DisplayHelp()
        Console.WriteLine("art2png [mode] [artdir] [pngdir] [palette] [(optional) verbose flag]")
        Console.WriteLine()
        Console.WriteLine("modes::" + vbTab + vbTab + "-export, -import, or -help")
        Console.WriteLine("artdir::" + vbTab + "Art tiles directory relative to current working directory")
        Console.WriteLine("pngdir::" + vbTab + "PNGs/INIs directory relative to current working directory")
        Console.WriteLine("palette::" + vbTab + "Palette directory (containing PALETTE.DAT) relative to current working directory")
        Console.WriteLine("verbose flag:: " + vbTab + "-v or -verbose. Echos diagnostic info to console. Is optional.")
        Console.WriteLine()
    End Sub

    ' This method was used so much I had to refactor it.
    Private Sub AnyKeyToExit()
        Console.WriteLine("Press Any Key To Exit.")
        Console.ReadKey()
    End Sub

End Module
